package com.songtech.base.universal.controller;

import java.sql.SQLException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.songtech.base.common.BaseConstant;
import com.songtech.base.common.JsonResult;
import com.songtech.base.exception.BusinessException;
import com.songtech.base.util.JsonUtil;

/**
 * 通用control
 * <br>传入的json包含的内容
 * <br>methodName：要调用的方法
 * <br>pageNum：页数，可选
 * <br>pageSize：显示的条数，可选
 * <br>param：mybatis的xml对应的参数
 */
@Controller
@RequestMapping("/universal")
public class UniversalController {
	@Autowired
	private UniversalService universalService;
	
	/**
	 * 通用添加
	 * @param param json字符串
	 * <br>methodName：要调用的方法
	 * <br>param：mybatis的xml对应的参数
	 * @return
	 */
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult insert(@RequestParam(value = "parameter", required = true) String param){
		UniversalParam uParam = this.setUniversalParam(param);
		int num = 0;
		try {
			num = universalService.insert(uParam.getMethodName(), uParam.getParam());
		} catch (SQLException e) {
			e.printStackTrace();
			throw new BusinessException(e.getMessage());
		}
		return new JsonResult(num, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 通用删除
	 * @param param json字符串
	 * <br>methodName：要调用的方法
	 * <br>param：mybatis的xml对应的参数
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult delete(@RequestParam(value = "parameter", required = true) String param){
		UniversalParam uParam = this.setUniversalParam(param);
		int num = 0;
		try {
			num = universalService.delete(uParam.getMethodName(), uParam.getParam());
		} catch (SQLException e) {
			e.printStackTrace();
			throw new BusinessException(e.getMessage());
		}
		return new JsonResult(num, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 通用修改
	 * @param param json字符串
	 * <br>methodName：要调用的方法
	 * <br>param：mybatis的xml对应的参数
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult update(@RequestParam(value = "parameter", required = true) String param){
		UniversalParam uParam = this.setUniversalParam(param);
		int num = 0;
		try {
			num = universalService.update(uParam.getMethodName(), uParam.getParam());
		} catch (SQLException e) {
			e.printStackTrace();
			throw new BusinessException(e.getMessage());
		}
		return new JsonResult(num, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 通用查询单条数据
	 * @param param json字符串
	 * <br>methodName：要调用的方法
	 * <br>param：mybatis的xml对应的参数
	 * @return
	 */
	@RequestMapping(value = "/selectOne", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult selectOne(@RequestParam(value = "parameter", required = true) String param){
		UniversalParam uParam = this.setUniversalParam(param);
		Object obj;
		try {
			obj = universalService.selectOne(uParam.getMethodName(), uParam.getParam());
		} catch (SQLException e) {
			e.printStackTrace();
			throw new BusinessException(e.getMessage());
		}
		return new JsonResult(obj, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 通用查询单条数据
	 * <br>使用redis缓存
	 * @param param json字符串
	 * <br>methodName：要调用的方法
	 * <br>param：mybatis的xml对应的参数
	 * @return
	 */
	@RequestMapping(value = "/selectOneUseRedis", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult selectOneUseRedis(@RequestParam(value = "parameter", required = true) String param){
		UniversalParam uParam = this.setUniversalParam(param);
		Object obj;
		try {
			obj = universalService.selectOneUseRedis(uParam.getMethodName(), uParam.getParam());
		} catch (SQLException e) {
			e.printStackTrace();
			throw new BusinessException(e.getMessage());
		}
		return new JsonResult(obj, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 通用查询多条数据，支持分页
	 * @param param json字符串
	 * <br>methodName：要调用的方法
	 * <br>pageNum：页数
	 * <br>pageSize：显示的条数
	 * <br>param：mybatis的xml对应的参数
	 * @return
	 */
	@RequestMapping(value = "/selectList", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult selectList(@RequestParam(value = "parameter", required = true) String param){
		UniversalParam uParam = this.setUniversalParam(param);
		PageInfo<?> page;
		try {
			page = universalService.selectList(uParam.getMethodName(), uParam.getParam(), uParam.getPage());
		} catch (SQLException e) {
			e.printStackTrace();
			throw new BusinessException(e.getMessage());
		}
		return new JsonResult(page, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 通用查询多条数据，支持分页
	 * @param param json字符串
	 * <br>methodName：要调用的方法
	 * <br>pageNum：页数，可选
	 * <br>pageSize：显示的条数，可选
	 * <br>param：mybatis的xml对应的参数
	 * @return
	 */
	@RequestMapping(value = "/selectListUseRedis", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult selectListUseRedis(@RequestParam(value = "parameter", required = true) String param){
		UniversalParam uParam = this.setUniversalParam(param);
		PageInfo<?> page;
		try {
			page = universalService.selectListUseRedis(uParam.getMethodName(), uParam.getParam(), uParam.getPage());
		} catch (SQLException e) {
			e.printStackTrace();
			throw new BusinessException(e.getMessage());
		}
		return new JsonResult(page, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 获取传入的参数
	 * @param param
	 * @return
	 */
	private UniversalParam setUniversalParam(String param){
		UniversalParam uParam = new UniversalParam();
		Map<String, Object> map = JsonUtil.readJsonValueToMap(param.replace("\"{", "{").replace("}\"", "}").replace("\\", ""));
		uParam.setMethodName(map.get("methodName"));
		uParam.setPageNum(map.get("pageNum"));
		uParam.setPageSize(map.get("pageSize"));
		uParam.setParam(map.get("param"));
		return uParam;
	}
}
